package com.lance.shiro.mapper;

import com.lance.shiro.entity.ISolicitor;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SolicitorMapper extends GenericCrudMapper<ISolicitor> {

    //insert
    @Insert("insert into i_solicitor" +
            "(company,phone,fax,email,address) " +
            "values" +
            "(#{company},#{phone},#{fax},#{email},#{address})")
    @Options(useGeneratedKeys = true)
    int insert(ISolicitor solicitor);

    //fetchAll
//    @Select("select * from i_solicitor")
    @Select("select a.*,ifnull(b.`agentId` is not null,true) as notifysale " +
            "from i_solicitor as a left JOIN `i_salesNotification` as b on a.id = b.`solicitorId` order by a.id")
    List<ISolicitor> fetchAll();

    //fetchByID
    @Select("select * from i_solicitor where id=#{id}")
    ISolicitor fetchById(int id);


    //update
    @Update("update i_solicitor set company=#{company}," +
            "phone=#{phone}, " +
            "fax=#{fax}, " +
            "email=#{email}, " +
            "address=#{address} " +
            "where id=#{id}")
    int update(ISolicitor solicitor);

    //delete
    @Update("delete from i_solicitor where id=#{id} ")
    int delete(int id);

    @Select("<script>" +
            "select * from i_solicitor " +
            "<where>" +
            "<if test='company!=null'>" +
            "  and company=#{company}" +
            "</if>" +
            "<if test='phone!=null'>" +
            "  and phone=#{phone}" +
            "</if>" +
            "<if test='fax!=null'>" +
            "  and fax=#{fax}" +
            "</if>" +
            "<if test='email!=null'>" +
            "  and email=#{email}" +
            "</if>" +
            "<if test='address!=null'>" +
            "  and address=#{address}" +
            "</if>" +
            "</where>" +
            "</script>")
    ISolicitor fetchByBean(ISolicitor solicitor);

    @Select("select * from i_solicitor where id in (${ids})")
    List<ISolicitor> findAllByIds(@Param("ids")String ids);
}


